package com.example.my3.ui.home;


import static android.net.wifi.WifiManager.*;

import androidx.core.app.NotificationCompat;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.example.my3.MainActivity;
import com.example.my3.databinding.FragmentHomeBinding;

import java.lang.reflect.Method;
import android.os.Build;
public class HomeFragment extends Fragment {

    private FragmentHomeBinding binding;
    private String TAG = "debug";
    private WifiManager wifiManager=null;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        /**
         * 参数1：布局文件的id
         * 参数2：容器
         * 参数3：是否将生成的这个view添加到这个容器中。
         * 作用是将布局文件封装在一个view对象中，并填充到此Fragment中。
         */

        wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);

        HomeViewModel homeViewModel =
                new ViewModelProvider(this).get(HomeViewModel.class);

        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        final TextView textView = binding.textHome;
        final Button button1 = binding.button1;
        final Button button2 = binding.button2;
        final Button btnHot = binding.btnHot;

        btnHot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                WifiManager wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
                // 开启Wi-Fi
                wifiManager.setWifiEnabled(true);
                // 创建热点配置
                WifiConfiguration wifiConfiguration = new WifiConfiguration();
                wifiConfiguration.SSID = "MyHotspot";
                wifiConfiguration.preSharedKey = "mypassword";
                wifiConfiguration.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
                wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);

                try {
                    // 通过反射调用方法创建热点
                    Method method = wifiManager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);
                    method.invoke(wifiManager, wifiConfiguration, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (((MainActivity) getActivity()).getDefaultDataSubId() != 1) {
                    ((MainActivity) getActivity()).setDefaultDataSubId(1);
                    Log.d(TAG, "wjz debug onClick: 切换到SIM1！！！");
                } else {
                    Log.d(TAG, "wjz debug onClick: 当前使用SIM1，无需切换！！！");
                }
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (((MainActivity) getActivity()).getDefaultDataSubId() != 2) {
                    ((MainActivity) getActivity()).setDefaultDataSubId(2);
                    Log.d(TAG, "wjz debug onClick: 切换到SIM2！！！");
                } else {
                    Log.d(TAG, "wjz debug onClick: 当前使用SIM2，无需切换！！！");
                }
            }
        });

        homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<Setting>() {
            @Override
            public void onChanged(Setting setting) {
                textView.setText(homeViewModel.getText().getValue().getTitle());
                Log.d(TAG, "you debug onClick: 切换到SIM1！！！");
            }
        });

        String version = Build.VERSION.RELEASE; // 获取当前设备的Android版本号
        System.out.println("Android版本号为：" + version);
        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    //传入参数：1-SIM1, 2-SIM2
    public void CheckState(View view) {

//        wifiManager.getWifiApConfiguration();
        //获取wifi AP状态
//        int state = wifiManager.getwifiApState();
//        if (state == WifiManager.WIFI_AP_STATE_ENABLED) {
//            // 热点已开启
//
//        } else if (state == WifiManager.WIFI_AP_STATE_DISABLED) {
//            // 热点已关闭
//        }
    }



}